package com.xfp.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xfp.mapper.PolicyMapper;
import com.xfp.method.pageTotal;
import com.xfp.pojo.dto.PageDto;
import com.xfp.pojo.dto.PolicyDto;
import com.xfp.pojo.dto.PolicyQueryDto;
import com.xfp.pojo.entity.Policy;
import com.xfp.pojo.entity.PolicyVm;
import com.xfp.pojo.vo.PolicyQueryVo;
import com.xfp.pojo.vo.PolicyVmQueryVo;
import com.xfp.pojo.vo.VmPolicyVo;
import com.xfp.service.PolicyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PolicyServiceImpl implements PolicyService {

    @Autowired
    private PolicyMapper policyMapper;

    /**
     * 搜索策略列表
     * @param dto
     * @return
     */
    @Override
    public PolicyQueryVo policyPage(PolicyQueryDto dto) {
        PageHelper.startPage(dto.getPageIndex(),dto.getPageSize());
        Page<Policy> page = policyMapper.policyPage(dto);
        return new PolicyQueryVo(dto.getPageIndex(),
                                 dto.getPageSize(),
                                 pageTotal.total(page.getTotal(),dto.getPageSize()),
                                 page.getTotal(),
                                 page.getResult());
    }

    /**
     * 新增策略
     * @param dto
     */
    @Override
    public void insertPolicy(PolicyDto dto) {
        policyMapper.insertPolicy(dto);
    }

    /**
     * 根据策略搜索售货机
     * @param dto
     * @return
     */
    @Override
    public PolicyVmQueryVo PolicyVmQuery(Integer id,PageDto dto) {
        PageHelper.startPage(dto.getPageIndex(),dto.getPageSize());
        Page<PolicyVm> page = policyMapper.PolicyVmQuery(id);
        return new PolicyVmQueryVo(dto.getPageIndex(),
                dto.getPageSize(),
                pageTotal.total(page.getTotal(),dto.getPageSize()),
                page.getTotal(),
                page.getResult());
    }

    /**
     * 修改策略
     * @param policyId
     * @param dto
     */
    @Override
    public void updatePolicy(Integer policyId, PolicyDto dto) {
        dto.setPolicyId(policyId);
        policyMapper.updatePolicy(dto);
    }

    /**
     * 删除策略
     * @param policyId
     */
    @Override
    public void deletePolicy(Integer policyId) {
        policyMapper.deletePolicy(policyId);
    }

    /**
     * 查询售货机策略
     * @param innerCode
     * @return
     */
    @Override
    public VmPolicyVo findVmPolicy(String innerCode) {
        VmPolicyVo vo = policyMapper.findVmPolicy(innerCode);
        return vo;
    }
}
